package com.heima.admin.logging;

import com.alibaba.fastjson.JSONObject;
import com.heima.common.log.AdminLog;
import com.heima.model.common.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @创建人: stone
 * @描述:
 */
@Component
@Slf4j
public class AdminLoggng {

    //异常数据处理
    @KafkaListener(topics = "ERR_MSG_LOG")
    public void AdminLoggngErr(ConsumerRecord<String, String> record) {
        AdminLog adminLog = ParameterProcessing(record);
        //存储数据库
        //.........
        log.info("切面日志 : " + adminLog);
    }

    //不是异常数据处理
    @KafkaListener(topics = "END_MSG_LOG")
    public void AdminLoggngEnd(ConsumerRecord<String, String> record) {
        AdminLog adminLog = ParameterProcessing(record);
        //存储数据库
        //.........
        log.info("切面日志 : " + adminLog);
    }

    //参数处理
    public AdminLog ParameterProcessing(ConsumerRecord<String, String> record) {

        String value = record.value();
        HashMap hashMap = JSONObject.parseObject(value, HashMap.class);
        String runMethod = (String) hashMap.get("runMethod");
        String calssName = (String) hashMap.get("calssName");
        Object args = hashMap.get("args");
        Exception exception = null;
        Integer operatingCrew = null;
        try {
            operatingCrew = Integer.valueOf(hashMap.get("operatingCrew") + "");
            exception = (Exception) hashMap.get("exception");
        } catch (Exception e) {

        }
        ResponseResult methodReturn = null;
        try {
            methodReturn = (ResponseResult) hashMap.get("methodReturn");
        } catch (Exception e) {

        }

        AdminLog adminLog = new AdminLog(runMethod, calssName, args, operatingCrew, exception, methodReturn);

        return adminLog;
    }
}
